﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace TCCUIControls
{
    public partial class UISharedMemory : UserControl
    {
        private int m_Padding = 5;
        private Brush m_Brush = new SolidBrush(Color.LightSteelBlue);
        private Brush m_WhiteBrush = new SolidBrush(Color.White);
        private Brush m_TextBrush = new SolidBrush(Color.Black);
        private Pen m_Pen = new Pen(Color.Gray);
        private string m_Name;

        public UISharedMemory(string name, CommandListView lstView)
        {
            m_Name = name;
            lstCommands = lstView;

            InitializeComponent();
        }

        private void pnlName_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            Rectangle rect = pnlName.ClientRectangle;
            rect.Width--;
            rect.Height--;

            g.FillRectangle(m_WhiteBrush, rect);
            g.DrawRectangle(m_Pen, rect);

            rect.X += m_Padding;
            rect.Y += m_Padding;
            rect.Width -= 2 * m_Padding;
            rect.Height -= 2 * m_Padding;

            g.FillRectangle(m_Brush, rect);
            g.DrawRectangle(m_Pen, rect);

            rect.X += m_Padding;
            rect.Y += m_Padding;
            rect.Width -= 2 * m_Padding;
            rect.Height -= 2 * m_Padding;

            SizeF textSize = g.MeasureString(m_Name, pnlName.Font);
            g.DrawString(m_Name, pnlName.Font, m_TextBrush, rect.X + rect.Width/2 - textSize.Width/2, rect.Y);
        }

        private delegate void UpdateMemoryDelegate(List<ListViewItem> buffer);
        public void UpdateMemory(List<ListViewItem> buffer)
        {
            if (lstCommands.InvokeRequired)
            {
                lstCommands.BeginInvoke(new UpdateMemoryDelegate(UpdateMemory), buffer);
                return;
            }

            lock (lstCommands)
            {
                lstCommands.Items.Clear();
                lstCommands.Items.AddRange(buffer.ToArray());
            }
            lstCommands.Invalidate();
        }
    }
}
